package com.wangzhixuan.commons.filestorage;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 对象存储 配置接口类
 *
 * @author lixin
 */
@Data
public class FileStorageConfig {
    /**
     * 开启状态
     */
    @ApiModelProperty("开启状态")
    protected boolean status = false;

    @ApiModelProperty(hidden = true)
    public FileStorageEnum getType() {
        return null;
    }

    public void mapToConfig(Map<String, String> configMap) {
        Field[] fields = getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                field.set(this, configMap.get(field.getName()));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        this.setStatus(Boolean.valueOf(Objects.toString(configMap.get("status"), "false")));
    }

    public Map<String, String> configToMap() {
        Field[] fields = getClass().getDeclaredFields();
        Map<String, String> configMap = new HashMap<>(fields.length + 1);
        for (Field field : fields) {
            try {
                configMap.put(field.getName(), (String) field.get(this));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        configMap.put("status", Boolean.toString(this.status));
        return configMap;
    }
}
